//
//  LLShoppingCartViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/9.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLShoppingCartViewController.h"
#import "LLShoppingCarCell.h"
#import "LLShoppingCartModel.h"
#import "LLShoppingCartHeader.h"
#import "LLShoppingCartFooter.h"
#import "LLShoppingCartBottomView.h"
#import "LLCommitOrderViewController.h"
#import "LLShopViewController.h"

@interface LLShoppingCartViewController ()<LLShoppingCartHeaderDelegate, LLShoppingCartBottomViewDelegate, LLShoppingCarCellDelegate>
@property (strong, nonatomic) LLShoppingCartBottomView *bottomView;
@end

@implementation LLShoppingCartViewController

- (void)viewDidLoad {
    self.tableViewStyle = UITableViewStyleGrouped;
    [super viewDidLoad];
    self.navTitle = @"购物车";
    self.rightNavTitle = @"管理";
    [self initData];
    [self initBottomView];
}

- (void)initData {
    for (NSInteger i = 0; i < 4; i++) {
        LLShoppingCartSectionModel *model = LLShoppingCartSectionModel.new;
        if (i % 3 == 0) {
            model.title = @"小提琴教育店";
        } else if (i % 3 == 1) {
            model.title = @"钢琴教育店";
        } else {
            model.title = @"教育店";
        }
        
        model.noteStr = @"5000积分换购";
        NSInteger count = i % 3 + 1;
        NSMutableArray *array = NSMutableArray.new;
        for (NSInteger j = 0; j < count; j++) {
            LLShoppingCartModel *model = LLShoppingCartModel.new;
            model.imgUrl = @"";
            if (j % 3 == 0) {
                model.title = @"满200-100元优惠券满满200-100元优惠券满满200-100元优惠券满满200-100元优惠券满";
                model.price = @"12";
                model.countStr = @"1";
            } else if (j % 3 == 1) {
                model.title = @"满200-100元优惠券";
                model.price = @"761.5";
                model.countStr = @"1";
            } else {
                model.title = @"满200-100元优惠券满满200-100元优惠券满满200-100元优惠券满";
                model.price = @"6.5";
                model.countStr = @"2";
            }
            [array addObject:model];
        }
        model.shoppingCarts = array;
        [self.models addObject:model];
    }
    [self.tableView reloadData];
}

- (void)initBottomView {
    if (!self.models.count) return;
    _bottomView = [[LLShoppingCartBottomView alloc] initWithFrame:CGRectMake(0, LLScreenHeight-70-self.navH, LLScreenWidth, 70)];
    _bottomView.backgroundColor = UIColor.whiteColor;
    _bottomView.delegate = self;
    [self.view addSubview:_bottomView];
}

- (void)configTableView {
    [super configTableView];
    self.tableView.height = LLScreenHeight-self.navH-70;
    self.tableView.backgroundColor = LLColorRGB(240);
    [self.tableView registerCellByClassName:@"LLShoppingCarCell"];
    self.tableView.rowHeight = 110;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
}

#pragma mark - UITableViewDataSource & UItableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    LLShoppingCartSectionModel *model = self.models[section];
    return model.shoppingCarts.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    LLShoppingCartHeader *header = [[LLShoppingCartHeader alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 50)];
    header.backgroundColor = LLColorRGB(240);
    header.model = self.models[section];
    header.delegate = self;
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    LLShoppingCartFooter *footer = [[LLShoppingCartFooter alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 50)];
    footer.backgroundColor = LLColorRGB(240);
    footer.model = self.models[section];
    return footer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LLShoppingCarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLShoppingCarCell"];
    LLShoppingCartSectionModel *sectionModel = self.models[indexPath.section];
    LLShoppingCartModel *model = sectionModel.shoppingCarts[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = LLColorRGB(240);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - LLShoppingCarCellDelegate
- (void)cellDidClickSelect:(LLShoppingCarCell *)cell {
    LLShoppingCartModel *model = cell.model;
    model.isSelected = model.isSelected ? 0 : 1;
    // 遍历所有模型数据
    [self traversingModels];
}

#pragma mark - LLShoppingCartHeaderDelegate

- (void)headerDidClickSelecte:(LLShoppingCartHeader *)header {
    LLShoppingCartSectionModel *sectionModel = header.model;
    sectionModel.isSelected = sectionModel.isSelected ? 0 : 1;
    
    for (LLShoppingCartModel *model in sectionModel.shoppingCarts) {
        model.isSelected = sectionModel.isSelected;
    }
    
    // 遍历所有模型数据
    [self traversingModels];
}

- (void)headerDidClickTitle:(LLShoppingCartHeader *)header {
    LLShopViewController *vc = LLShopViewController.new;
    vc.navTitle = header.model.title;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma marl - LLShoppingCartBottomViewDelegate

- (void)bottomViewDidClickSelecte:(LLShoppingCartBottomView *)bottomView {
    bottomView.allSelected = !bottomView.allSelected;
    NSMutableArray *selectedArr = NSMutableArray.new;
    for (LLShoppingCartSectionModel *sectionModel in self.models) {
        sectionModel.isSelected = bottomView.allSelected;
        for (LLShoppingCartModel *model in sectionModel.shoppingCarts) {
            model.isSelected = bottomView.allSelected;
            if (model.isSelected) [selectedArr addObject:model];
        }
    }
    self.bottomView.countStr = [NSString stringWithFormat:@"%ld",selectedArr.count];
    [self.tableView reloadData];
}

- (void)bottomViewDidClickSettle {
    LLCommitOrderViewController *vc = LLCommitOrderViewController.new;
    vc.models = self.models;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - private method

// 遍历所有模型数据
- (void)traversingModels {
    BOOL selectedAll = YES;
    NSMutableArray *selectedArr = NSMutableArray.new;
    for (NSInteger i = 0; i < self.models.count; i++) {
        LLShoppingCartSectionModel *sectionModel = self.models[i];
        NSMutableArray *sectionArr = NSMutableArray.new;
        for (NSInteger j = 0; j < sectionModel.shoppingCarts.count; j++) {
            LLShoppingCartModel *model = sectionModel.shoppingCarts[j];
            if (model.isSelected) {
                [selectedArr addObject:model];
                [sectionArr addObject:model];
            }
        }
        sectionModel.isSelected = sectionArr.count == sectionModel.shoppingCarts.count;
        if (!sectionModel.isSelected) {
            selectedAll = NO;
        }
    }
    
    self.bottomView.allSelected = selectedAll;
    self.bottomView.countStr = [NSString stringWithFormat:@"%ld",selectedArr.count];
    [self.tableView reloadData];
}
@end
